﻿Public Class t01_rute
    Inherits DevExpress.XtraEditors.XtraForm
    Dim acr As New ModLibrary.All_Function_Cls
    Public BS As BindingSource
    Private GoogleControl2 As GoogleControl2
    Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        GoogleControl2 = New GoogleControl2
        GoogleControl2.Dock = DockStyle.Fill
        'Me.Controls.Add(GoogleControl1)
        Me.Panel1.Controls.Add(GoogleControl2)
    End Sub
    Private Sub t01_rute_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ID_TITIKTextBox.Text = t03_master_titik.ID_TITIKTextBox.Text
        DevExpress.Skins.SkinManager.EnableFormSkins()
        DevExpress.UserSkins.BonusSkins.Register()
        DevExpress.UserSkins.OfficeSkins.Register()
        Me.LookAndFeel.SkinName = "Office 2010 Blue"
        DefaultLookAndFeel1.LookAndFeel.SkinName = "Office 2010 Blue"
        LoadNow()
    End Sub

    Sub LoadNow()
        'TODO: This line of code loads data into the 'DS_t01_rute.rute' table. You can move, or remove it, as needed.
        BS = RuteBindingSource
        ID_TITIKTextBox.Text = t03_master_titik.ID_TITIKTextBox.Text
        Me.RuteTableAdapter.Fill(Me.DS_t01_rute.rute)
        BS.MoveLast()
    End Sub

    Function Form_Valid()
        Dim Pesan, Validasi As String
        Pesan = "" : Validasi = ""
        'If ID_TITIKTextBox.Text = "" Then Pesan += "- ID_Titik Cannot Empty " & vbNewLine
        'If ID_RUTETextBox.Text = "" Then Pesan += "- ID_Rute Month Cannot Empty " & vbNewLine
        If NAMA_LOKASITextBox.Text = "" Then Pesan += "- Nama Lokasi tidak boleh kosong " & vbNewLine
        If LONGITUDETextBox.Text = "" Then Pesan += "- Longitude tidak boleh kosong " & vbNewLine
        If LATITUDETextBox.Text = "" Then Pesan += "- Latitude tidak boleh kosong " & vbNewLine
        If STATUSComboBox.Text = "" Then Pesan += "- Status tidak boleh kosong " & vbNewLine
        If Pesan <> "" Then
            Validasi = "Input Data Is Not Correct / Not Complete on : " & vbNewLine & vbNewLine & Pesan
        End If
        Return Validasi
    End Function


    Sub Save()
        Try
            If Form_Valid() = "" Then
                Me.Validate()
                Me.RuteBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.DS_t01_rute)
                MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
                LoadNow()
            Else
                MsgBox(Form_Valid, MsgBoxStyle.Critical, "Error Data Saving")
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Sub Save_Special()
        Try
        Me.Validate()
        Me.RuteBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DS_t01_rute)
        MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
            LoadNow()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Sub Delete()
        If MsgBox("Delete this data ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Delete Confirmation") = MsgBoxResult.Yes Then
            BS.RemoveCurrent()
            If MsgBox("if you are sure... click [Yes] button to delete and save this changes..", MsgBoxStyle.Information + MsgBoxStyle.OkCancel, "Pending Delete Data") = MsgBoxResult.Ok Then
                If Form_Valid() = "" Then
                    Save()
                Else
                    Save_Special()
                End If
            Else
                acr.MoveNavi("Load", 0, Me, BS)
            End If
        End If
    End Sub

    Private Sub Btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Save.Click, Btn_Refresh.Click, Btn_Previous.Click, Btn_Next.Click, Btn_Last.Click, Btn_First.Click, Btn_Delete.Click, Btn_Add.Click
        Select Case CType(sender, Button).Name.ToString
            Case "Btn_Save"
                Save()
            Case "Btn_Refresh"
                acr.MoveNavi("Load", 0, Me, BS)
            Case "Btn_Previous"
                acr.MoveNavi("MovePrev", 0, Me, BS)
            Case "Btn_Next"
                acr.MoveNavi("MoveNext", 0, Me, BS)
            Case "Btn_Last"
                acr.MoveNavi("MoveLast", 0, Me, BS)
            Case "Btn_First"
                acr.MoveNavi("MoveFirst", 0, Me, BS)
            Case "Btn_Add"
                acr.MoveNavi("Add", 0, Me, BS)
            Case "Btn_Delete"
                Delete()
        End Select
    End Sub

    Private Sub ruteDataGridView_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles RuteDataGridView.DataError
        e.Cancel = True
    End Sub

    Private Sub LabelControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelControl1.Click

    End Sub

    Private Sub LONGITUDETextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LONGITUDETextBox.TextChanged

    End Sub
End Class
'The Control

'The class needs to be marked as com visible so the

'script in the GoogleMap.htm can call some of the subs.

<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class GoogleControl2 : Inherits UserControl

    Private WebBrowser1 As WebBrowser

    Private Panel As Panel

    Private StatusStrip1 As StatusStrip

    Private StatusButtonDelete As ToolStripButton

    Private StatusLabelLatLng As ToolStripStatusLabel

    Private InitialZoom As Integer

    Private InitialLatitude As Double

    Private InitialLongitude As Double

    Private InitialMapType As GoogleMapType

    'I use this enum to store the current map

    'type into the application's settings.

    'So when the user closes the map on Satellite

    'mode it will be on Satellite mode the next 

    'time they open it.

    Public Enum GoogleMapType

        None

        RoadMap

        Terrain

        Hybrid

        Satellite

    End Enum

    Sub New()

        MyBase.New()

        WebBrowser1 = New WebBrowser

        StatusStrip1 = New StatusStrip

        StatusButtonDelete = New ToolStripButton

        StatusLabelLatLng = New ToolStripStatusLabel

        WebBrowser1.Dock = DockStyle.Fill

        WebBrowser1.AllowWebBrowserDrop = False

        WebBrowser1.IsWebBrowserContextMenuEnabled = False

        WebBrowser1.WebBrowserShortcutsEnabled = False

        WebBrowser1.ObjectForScripting = Me

        WebBrowser1.ScriptErrorsSuppressed = False

        AddHandler WebBrowser1.DocumentCompleted, AddressOf WebBrowser1_DocumentCompleted

        StatusStrip1.Dock = DockStyle.Bottom

        StatusStrip1.Items.Add(StatusButtonDelete)

        StatusStrip1.Items.Add(StatusLabelLatLng)

        StatusButtonDelete.Text = "Delete Markers"

        AddHandler StatusButtonDelete.Click, AddressOf StatusButtonDelete_Click

        Me.Controls.Add(WebBrowser1)

        Me.Controls.Add(StatusStrip1)

        'The default map settings.

        InitialZoom = 9

        InitialLatitude = -7.553101

        InitialLongitude = 112.282805

        InitialMapType = GoogleMapType.RoadMap

    End Sub

    Sub New(ByVal zoom As Integer, ByVal lat As Double, ByVal lng As Double, ByVal mapType As GoogleMapType)

        'This constructor could be used to start the map with different values

        'other than the default settings.

        Me.New()

        InitialZoom = zoom

        InitialLatitude = lat

        InitialLongitude = lng

        InitialMapType = mapType

    End Sub

    Private Sub GoogleControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Load the htm doc into the webrowser.

        'When it completes, intialize the map.

        WebBrowser1.DocumentText = My.Computer.FileSystem.ReadAllText("GoogleMap.htm")

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

        'Initialize the google map with the initial settings.

        'The Initialize script function takes four parameters.

        'zoom, lat, lng, maptype. Call the script passing the

        'parameters in.

        WebBrowser1.Document.InvokeScript("Initialize", New Object() {InitialZoom, InitialLatitude, InitialLongitude, CInt(InitialMapType)})

    End Sub

    Public Sub Map_MouseMove(ByVal lat As Double, ByVal lng As Double)

        'Called from the GoogleMap.htm script when ever the mouse is moved.

        StatusLabelLatLng.Text = "lat/lng: " & CStr(Math.Round(lat, 4)) & " , " & CStr(Math.Round(lng, 4))

        'Form1.txtLong.Text = CStr(Math.Round(lat, 4))

        'Form1.txtLat.Text = CStr(Math.Round(lng, 4))



    End Sub

    Public Sub Map_Click(ByVal lat As Double, ByVal lng As Double)

        'Add a marker to the map.

        'Dim MarkerName As String = MsgBox("Success !!")

        'If Not String.IsNullOrEmpty(MarkerName) Then

        'The script function AddMarker takes three parameters

        'name,lat,lng. Call the script passing the parameters in.
        'Dim rute As New t01_rute
        t01_rute.LATITUDETextBox.Text = CStr(Math.Round(lat, 4))

        t01_rute.LONGITUDETextBox.Text = CStr(Math.Round(lng, 4))
        'MsgBox(t01_rute.LATITUDETextBox.Text & " / " & t01_rute.LONGITUDETextBox.Text)
        'MsgBox(CStr(Math.Round(lng, 4)) & CStr(Math.Round(lat, 4)))

        'WebBrowser1.Document.InvokeScript("AddMarker", New Object() {MarkerName, lat, lng})

        'End If

    End Sub

    Public Sub Map_Idle()

        'Would be a good place to load your own custom markers

        'from data source

    End Sub

    Public Sub test(ByVal start As String, ByVal akhir As String, ByVal wp As String)

        'MsgBox("TreeViewHitTestInfo")

        WebBrowser1.Document.InvokeScript("calcRoute", New Object() {start, akhir, wp})

        'MsgBox(akhir)

    End Sub

    Public Sub test2(ByVal start As String, ByVal akhir As String, ByVal wp As String)

        MsgBox(akhir & start)

    End Sub

    Private Sub StatusButtonDelete_Click(ByVal sender As Object, ByVal e As EventArgs)

        'Call the DeleteMarkers script in the htm doc.

        WebBrowser1.Document.InvokeScript("DeleteMarkers")

    End Sub

End Class